package com.bootdo.common.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.domain.OssStorageDO;
import com.bootdo.common.service.OssStorageService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-07-23 20:20:02
 */
 
@Controller
@RequestMapping("/common/ossStorage")
public class OssStorageController extends BaseController{
	@Autowired
	private OssStorageService ossStorageService;
	
	@GetMapping()
	@RequiresPermissions("common:ossStorage:ossStorage")
	String OssStorage(){
	    return "common/ossStorage/ossStorage";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:ossStorage:ossStorage")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OssStorageDO> ossStorageList = ossStorageService.list(query);
		int total = ossStorageService.count(query);
		PageUtils pageUtils = new PageUtils(ossStorageList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/listView")
	public Object listView(){
		//查询列表数据
		List<OssStorageDO> ossStorageList = ossStorageService.list2();
		return ossStorageList;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("common:ossStorage:add")
	String add(){
	    return "common/ossStorage/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:ossStorage:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		OssStorageDO ossStorage = ossStorageService.get(id);
		model.addAttribute("ossStorage", ossStorage);
	    return "common/ossStorage/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:ossStorage:add")
	public R save( OssStorageDO ossStorage){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(ossStorageService.save(ossStorage)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:ossStorage:edit")
	public R update( OssStorageDO ossStorage){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		ossStorageService.update(ossStorage);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("common:ossStorage:remove")
	public R remove( Integer id){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(ossStorageService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:ossStorage:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		ossStorageService.batchRemove(ids);
		return R.ok();
	}


	
}
